var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
define(["require", "exports"], function (require, exports) {
    "use strict";
    var BackgroundGaussianBlur = (function (_super) {
        __extends(BackgroundGaussianBlur, _super);
        function BackgroundGaussianBlur(sprite, blur, quality, highlight) {
            if (blur === void 0) { blur = 20; }
            if (quality === void 0) { quality = 16; }
            if (highlight === void 0) { highlight = 1; }
            var maskMatrix = new PIXI.Matrix();
            /*
             * 顶点作色器
             */
            var vert = "attribute vec2 aVertexPosition;\nattribute vec2 aTextureCoord;\nattribute vec4 aColor;\n\nuniform vec2 delta;\nuniform mat3 projectionMatrix;\nuniform mat3 otherMatrix;\n\nvarying vec2 vMapCoord;\nvarying vec2 vTextureCoord;\nvarying vec4 vColor;\nvarying vec2 vDelta;\nvarying mat3 vOtherMatrix;\n\nvoid main(void)\n{\n\tgl_Position = vec4((projectionMatrix * vec3(aVertexPosition, 1.0)).xy, 0.0, 1.0);\n\tvTextureCoord = aTextureCoord;\n\n\tvDelta = delta;\n    vOtherMatrix = otherMatrix;\n    \n    vMapCoord = ( otherMatrix * vec3( aTextureCoord, 1.0)  ).xy;\n\n\tvColor = vec4(aColor.rgb * aColor.a, aColor.a);\n}";
            /*
             * 片元作色器
             */
            var frag = "precision mediump float;\n\nvarying vec2 vMapCoord;\nvarying vec2 vTextureCoord;\nvarying vec2 vDelta;\nvarying vec4 vColor;\nvarying mat3 vOtherMatrix;\n\nuniform sampler2D uSampler;\nuniform sampler2D mapSampler;\n\nfloat random(vec3 scale, float seed) {\n    /* use the fragment position for a different seed per-pixel */\n    return fract(sin(dot(gl_FragCoord.xyz + seed, scale)) * 43758.5453 + seed);\n}\n\nvoid main() {\n    vec4 map = texture2D(mapSampler, vMapCoord) ;\n    if( map.a==0.0){\n        gl_FragColor = texture2D(uSampler, vTextureCoord) * vColor ;\n    }else{\n        vec4 color = vec4(0.0);\n        float total = 0.0;\n        \n        /* randomize the lookup values to hide the fixed number of samples */\n        float offset = random(vec3(12.9898, 78.233, 151.7182), 0.0);\n        \n        for (float t = -" + quality + ".0; t <= " + quality + ".0; t++) {\n            float percent = (t + offset - 0.5) / " + quality + ".0;\n            float weight = 1.0 - abs(percent);\n            vec4 sample = texture2D(uSampler, vTextureCoord + vDelta * percent);\n            vec4 edge = texture2D(mapSampler, vMapCoord + vDelta * percent);\n            \n            if (edge.a > 0.0) {\n                /* switch to pre-multiplied alpha to correctly blur transparent images */\n                sample.rgb *= sample.a;\n\n                sample.rgb *= float(" + highlight + ");\n                \n                color += sample * weight;\n                total += weight;\n            }\n        }\n        \n        gl_FragColor = color / total;\n        \n        /* switch back from pre-multiplied alpha */\n        gl_FragColor.rgb /= gl_FragColor.a + 0.00001;\n    }\n}";
            _super.call(this, vert, frag, {
                delta: {
                    type: 'v2',
                    value: { x: 0, y: 0 },
                },
                mapSampler: {
                    type: 'sampler2D',
                    value: sprite.texture
                },
                otherMatrix: {
                    type: 'mat3',
                    value: maskMatrix.toArray(true)
                },
            });
            this._delta = blur;
            // console.log(sprite.texture);
            this.maskSprite = sprite;
            this.maskMatrix = maskMatrix;
        }
        BackgroundGaussianBlur.Wrap = function (child) {
            var res = new PIXI.Container();
            // 创建一个有透明边缘的容器老包裹贴图对象，来规避贴图贴边引起的问题。
            child.parent && child.addChild(res);
            res.addChild(child);
            child.x = 1;
            child.y = 1;
            res.width = child.width + 2;
            res.height = child.height + 2;
            return res;
        };
        BackgroundGaussianBlur.ContainerToSprite = function (con, renderer, resolution, scaleMode) {
            var _old_cacheAsBitmap = con.cacheAsBitmap;
            con.cacheAsBitmap = true;
            var sprite = new PIXI.Sprite(con.generateTexture(renderer, resolution, scaleMode));
            con.cacheAsBitmap = _old_cacheAsBitmap;
            return sprite;
        };
        BackgroundGaussianBlur.prototype.applyFilter = function (renderer, input, output, clear) {
            var filterManager = renderer.filterManager;
            var shader = this.getShader(renderer);
            var _delta_width = this._delta / input.size.width;
            var _delta_height = this._delta / input.size.height;
            filterManager.calculateMappedMatrix(input.frame, this.maskSprite, this.maskMatrix);
            this.uniforms.otherMatrix.value = this.maskMatrix.toArray(true);
            var renderTarget = filterManager.getRenderTarget(true);
            this.uniforms.delta.value = {
                x: _delta_width,
                y: 0,
            };
            filterManager.applyFilter(shader, input, renderTarget, clear);
            this.uniforms.delta.value = {
                x: 0,
                y: _delta_height,
            };
            filterManager.applyFilter(shader, renderTarget, output, clear);
            // 很重要，不加的话会引发内存泄漏
            filterManager.returnRenderTarget(renderTarget);
        };
        Object.defineProperty(BackgroundGaussianBlur.prototype, "blur", {
            // applyFilter(renderer, input, output, clear) {
            //     var filterManager = renderer.filterManager;
            //     var shader = this.getShader(renderer);
            //     // var renderTarget = filterManager.getRenderTarget(true);
            //     // filterManager.applyFilter(shader, input, renderTarget, clear);
            //     // this.blurFilter.applyFilter(renderer, renderTarget, output, clear);
            //     // filterManager.returnRenderTarget(renderTarget);
            //     // 把前面的渲染的渲染结果映射到遮罩矩阵中
            //     filterManager.calculateMappedMatrix(input.frame, this.maskSprite, this.maskMatrix);
            //     debugger
            //     this.uniforms.otherMatrix.value = this.maskMatrix.toArray(true);
            //     this.uniforms.scale.value.x = this.scale.x * (1 / input.frame.width);
            //     this.uniforms.scale.value.y = this.scale.y * (1 / input.frame.height);
            //     filterManager.applyFilter(shader, input, output);
            // }
            get: function () {
                return this._delta;
            },
            set: function (value) {
                this._delta = value;
            },
            enumerable: true,
            configurable: true
        });
        return BackgroundGaussianBlur;
    }(PIXI.AbstractFilter));
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.default = BackgroundGaussianBlur;
});
